package com.frxuexi.user.viewmodel

import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import com.frxuexi.lib_base.BaseViewModel
import com.frxuexi.lib_base.HttpType
import com.frxuexi.user.intent.UserIntent
import com.frxuexi.user.paging.MyDataSource
import com.frxuexi.user.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class UserViewModel @Inject constructor(private val userRepository: UserRepository):BaseViewModel<UserIntent>() {
    override fun handleIntent(it: UserIntent) {
        when(it){
            is UserIntent.register->{
                roomRequest(userRepository.register(it.user))
            }
            is UserIntent.login->{
                roomRequest(userRepository.login(it.uname,it.upass,it.role))
            }
            is UserIntent.addAsk->{
                roomRequest(userRepository.addAsk(it.askLeave))
            }

            else -> {}
        }
    }

    fun getData()=Pager(PagingConfig(pageSize = 1)){
        MyDataSource(userRepository)
    }.flow.cachedIn(viewModelScope)
}